##  Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 
##
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os, sys
import shutil

sys.path+=["."]
from pytriang import build


ext_source = ["src/ext/component.cpp",
              "src/ext/textedit.cpp",
              "src/ext/label.cpp",
              "src/ext/button.cpp",
              "src/ext/style.cpp",
              "src/ext/checkbox.cpp",
              "src/ext/panel.cpp",
              "src/ext/sliderbar.cpp",
              "src/ext/scrollpanel.cpp",
              "src/ext/numberedit.cpp",
#              "src/ext/tabpanel.cpp",
              "src/ext/popup.cpp",
              "src/ext/itemlist.cpp",
              #"src/ext/combobox.cpp",
              "src/ext/uistyle.cpp",
              "src/ext/spinbox.cpp"
#              
#              "src/ext/filebrowser.cpp"
              ]

base_source = ["src/base/vector.cpp",
                "src/base/utils.cpp" ,
                "src/base/animate.cpp",
                "src/base/object.cpp",
                "src/base/string.cpp",
                "src/base/timestamp.cpp", 
                "src/base/timer.cpp",
                "src/base/matrix.cpp", 
                "src/base/system.cpp",
                "src/base/rectangle.cpp", 
                "src/base/exception.cpp",
                "src/base/xmlnode.cpp",
                "src/base/threads.cpp",
                "src/base/logging.cpp",
#                
#
                "src/base/tinyxml/tinystr.cpp", 
                "src/base/tinyxml/tinyxml.cpp", 
                "src/base/tinyxml/tinyxmlparser.cpp", 
                "src/base/tinyxml/tinyxmlerror.cpp"
               ]



source_files = ["src/glut_common.cpp", 
                "src/context.cpp", 
                "src/graphics.cpp", 
                "src/glyph.cpp", 
                "src/texture.cpp",
                "src/entity.cpp", 
                "src/camera.cpp", 
                "src/display_engine.cpp", 
                "src/scene.cpp",
                "src/mouse_event.cpp",
                "src/key_event.cpp",
                "src/events.cpp",
                "src/world.cpp",
                "src/tile.cpp",
                "src/model3d.cpp",
                "src/particle_engine.cpp",
#                "src/3d_object.cpp",
#                "src/3d_object_loader.cpp",
                "src/mesh.cpp",
                "src/FreeType.cpp",
                "src/text.cpp",
                "src/common.cpp",
                "src/shapes.cpp",
                "src/transform.cpp",
                "src/material.cpp",
#                "src/template_scene.cpp"
                ]

tests = ['test_events']

def MKDIR(d):
    try:
        os.mkdir(d)
    except:
        pass


"""
Add Font library
"""
def addFreeTypeFont(prj, trg=None):
    if sys.platform == 'win32':
        include = [r'C:\\Triangulum\\GnuWin32\\include',r'C:\\Triangulum\\GnuWin32\\include\\freetype2']
        lib_dirs = [r'C:\\Triangulum\\GnuWin32\\lib']
        libs = ['freetype']
        cpp = []
        flags = []
        macros = []
        
        prj.add(trg, 
                CPP = cpp, 
                FLAGS = flags, 
                MACRO = macros, 
                INC = include, 
                LIB_DIR = lib_dirs, 
                LIBS = libs)
    else:
        include = [r'/usr/include/freetype2']
        lib_dirs = []
        libs = []
        cpp = []
        flags = []
        macros = []
        
        prj.add(trg, 
                CPP = cpp, 
                FLAGS = flags, 
                MACRO = macros, 
                INC = include, 
                LIB_DIR = lib_dirs, 
                LIBS = libs)
        
"""
Add the Triangulum library to the 
project build path
"""    
def addTriangulum(prj, trg=None):
    if sys.platform == 'win32':
        include = [r'C:\\Triangulum\\Triangulum\\include']
        lib_dirs = [r'C:\\Triangulum\\Triangulum\\lib']
        libs = ['triangulum', 'ws2_32']
        cpp = []
        flags = []
        macros = []
        
        prj.add(trg, 
                CPP = cpp, 
                FLAGS = flags, 
                MACRO = macros, 
                INC = include, 
                LIB_DIR = lib_dirs, 
                LIBS = libs)

"""
Add GLUT library to project
"""
def addGlut(prj, trg=None):
    if sys.platform == 'win32':
        include = [r'C:\\Triangulum\\glut\\glut\\mingw']
        lib_dirs = [r'C:\\Triangulum\\glut\\glut\\mingw']
        libs = ['glut32','opengl32','winmm','gdi32', 'glu32']
        cpp = []
        flags = ['O3','Wall','fmessage-length=0']
        macros = []
        
        prj.add(trg, 
                CPP = cpp, 
                FLAGS = flags, 
                MACRO = macros, 
                INC = include, 
                LIB_DIR = lib_dirs, 
                LIBS = libs)


def addPThreads(prj,trg=None):
    if sys.platform == 'win32':
        include = [r'C:\\Triangulum\\Pthreads-win32\\Pre-built.2\\include']
        lib_dirs = [r'C:\\Triangulum\\Pthreads-win32\\Pre-built.2\\lib']
        libs = ['pthreadGC2']
        cpp = []
        flags = ['O3','Wall','fmessage-length=0']
        macros = []
        
        prj.add(trg,
                CPP = cpp,
                FLAGS = flags,
                MACRO = macros,
                INC = include,
                LIB_DIR = lib_dirs,
                LIBS = libs)


def addDependencies(prj,trg):
    addPThreads(prj, trg)
    addGlut(prj, trg)
    addFreeTypeFont(prj,trg)
    pass

def create_test(prj, trg):
    MKDIR('release/tests')
    prj.addTarget(trg, 'release/tests/'+trg)
    prj.setMetaData(trg, discript = 'Unit Test ', icon = 'resources/triang.ico')

    prj.add(trg, CPP=['tests/'+trg+'.cpp'])
    addDependencies(prj,trg)

   

def create_unit_tests(prj):
    create_test(prj, 'test_events')
   

def create_test_app(prj, trg):
    prj.addTarget(trg, 'trg')
    prj.setMetaData(trg, discript = 'demo application ', icon = 'resources/triang.ico')
  
    addDependencies(prj,'trg')
    prj.add(trg, CPP="main.cpp")
    
def create_main_lib(prj):
    prj.addTarget('lib', 
                  OUT = 'release/lib/Triangulum', 
                  TYPE = make.STATIC_LIB)
    prj.add(CPP=source_files)
    addDependencies(prj,'lib')
#    create_test_app(prj, 'demo')
#    libs.addGLUT(prj, 'demo')
   

def main(argv):

    install_prefix = 'release'
    if len(argv)>2:
        install_prefix = argv[2]
    if os.path.isdir('release'):
        shutil.rmtree('release')
    
    prj = build.Make('make'+sys.platform)

    #CREATE LIB TARGET
    outdir = '%s/lib'%(install_prefix)
    prj.addTarget('lib',TYPE=build.STATIC_LIB,OUTDIR=outdir,OUT='Triangulum')
    prj.add('lib',CPP=base_source)
    prj.add('lib',CPP=source_files)
    prj.add('lib',CPP=ext_source)
    #prj.add('lib',CPP=physics_source)
    addDependencies(prj,'lib')
    if len(argv)>2:
        if argv[2] == "clean":
            prj.clean(argv[1])
            return 


    
    MKDIR('%s' %(install_prefix))
    MKDIR('%s/include'%(install_prefix))
    MKDIR('%s/include/stb'%(install_prefix))
    MKDIR('%s/include/stb/include'%(install_prefix))
    MKDIR('%s/include/ext'%(install_prefix))
    MKDIR('%s/include/base'%(install_prefix))
    MKDIR('%s/include/base/tinyxml'%(install_prefix))
    MKDIR('%s/include/sigslot'%(install_prefix))
    MKDIR('%s/lib'%(install_prefix))
    copy = 'copy'
    if 'linux' in sys.platform:
        os.system('cp src/*.h %s/include'%(install_prefix))
        os.system('cp src/stb/include/*.h %s/include/stb/include'%(install_prefix))
        os.system('cp src/base/tinyxml/*.h %s/include/base/tinyxml'%(install_prefix))
        os.system('cp src/base/*.h %s/include/base'%(install_prefix))
        os.system('cp src/ext/*.h %s/include/ext'%(install_prefix))
        os.system('cp src/sigslot/*.h %s/include/sigslot'%(install_prefix))
    else:
        os.system('copy src\\*.h %s\\include'%(install_prefix))
        os.system('copy src\\stb\\include\\*.h %s\\include\\stb\\include'%(install_prefix))
        os.system('copy src\\base\\tinyxml\\*.h %s\\include\\base\\tinyxml'%(install_prefix))
        os.system('copy src\\base\\*.h %s\\include\\base\\'%(install_prefix))
        os.system('copy src\\ext\\*.h %s\\include\\ext\\'%(install_prefix))
        os.system('copy src\\sigslot\\*.h %s\\include\\sigslot'%(install_prefix))
    

    if len(argv)>1:
        prj.build(argv[1])
    else:
        prj.build('lib')
 

#=============================================

if __name__ == '__main__':
    
    main(sys.argv)
    
